﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Lending.RMF.Tool;
using System.Threading;
using System.Text.RegularExpressions;
using System.Net;
namespace AutoRequest
{
    public class ganjiResource : IResource
    {
        private const string Prefix = "http://cq.ganji.com";
        private const string baseImgDir = "/content/resource/phoneImg/";
        private static Random random = new Random();
        public DateTime StartTime = DateTime.Now;
        public Dictionary<int, string> Get
        {
            get
            {
                return this.dict;
            }
        }
        public ganjiResource(string constr, DateTime startTime)
            : base(constr)
        {
            this.StartTime = startTime;
            if (!Directory.Exists(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "/content/resource/phoneImg/"))
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "/content/resource/phoneImg/");
            }
        }
        protected override void SetDict()
        {
            this.dict.Add(1, "/jzcuxiaoyuan/");
            this.dict.Add(4, "/jzjiajiaolaoshi/");
            this.dict.Add(5, "/jzshougongzhizuo/");
            this.dict.Add(6, "/jzfuwuyuan/");
            this.dict.Add(7, "/jzxuesheng/");
            this.dict.Add(8, "/jzchuandanpaifa/");
            this.dict.Add(9, "/jzwangzhanjianshe/");
            this.dict.Add(10, "/jzjianzhikuaiji/");
            this.dict.Add(11, "/jzshixisheng/");
            this.dict.Add(12, "/jzxiaoshigong/");
            this.dict.Add(13, "/jzjianzhifanyi/");
            this.dict.Add(14, "/jzwenjuandiaocha/");
            this.dict.Add(15, "/jzqunzhongyanyuan/");
            this.dict.Add(16, "/jzxiaoshou/");
            this.dict.Add(17, "/jzshejizhizuo/");
            this.dict.Add(18, "/jzsheyingshi/");
            this.dict.Add(19, "/qitajianzhi/");
        }
        public override void Do()
        {
            foreach (KeyValuePair<int, string> current in this.Get)
            {
                try
                {
                    LogHelper.Write(CommonLogger.Application, LogLevel.Info, "request:http://cq.ganji.com" + current.Value);
                    string html = Common.DoRequest("http://cq.ganji.com" + current.Value);
                    this.Analysts(html, current.Value, ref this.total);
                    Thread.Sleep(100);
                }
                catch (Exception ex)
                {
                    LogHelper.Write(0, 0, ex, "");
                }
            }
        }
        protected override void Analysts(string html, string uri, ref int count)
        {
            string input = base.Pickbody(html);
            MatchCollection matchCollection = new Regex("\\<dl\\s+class=\"list-noimg\\s+job-list\\s*\".*?\\>.*?\\<\\/dl\\>").Matches(input);
            foreach (Match match in matchCollection)
            {
                Thread.Sleep(1000);
                count++;
                try
                {
                    Info info = new Info();
                    string value = match.Value;
                    string value2 = new Regex("(?<=name=\"send_resume\"/?>\\s*<a\\s*(class=\"list_title\"\\s*)?href=\").*?(?=\")").Match(value).Value;
                    info.orgId = value2;
                    string html2 = Common.DoRequest("http://cq.ganji.com" + value2);
                    string text = base.Pickbody(html2);
                    info.Caption = new Regex("(?<=<h1\\s+class=\"f24\\s+fc4b\\s+h31\">).*?(?=</h1>)").Match(text).Value.EmptyHTMLLabel();
                    if (string.IsNullOrEmpty(info.Caption.Trim()))
                    {
                        this.jumpwitherror++;
                    }
                    else
                    {
                        string value3 = new Regex("(?<=<p\\s+class=\"fc70\\s+mb-5\">.*?)\\d{2}-\\d{2}\\s\\d{1,2}\\:\\d{1,2}(?=</span>)").Match(text).Value;
                        DateTime t = DateTime.Parse((DateTime.Now.Year.ToString() + "-" + value3));
                        if (t < this.StartTime)
                        {
                            this.jumpwithtime++;
                        }
                        else
                        {
                            info.ContactMan = new Regex("(?<=<dd>联\\s*系\\s*人：).*?(?=\\s)").Match(text).Value.NoHTMLLabel();
                            if (text.Contains("联系电话："))
                            {
                                Match match2 = new Regex("(?<=<dd.*?>联系电话：.*?).*?(?=</dd>)").Match(text);
                                if (!string.IsNullOrEmpty(match2.Value))
                                {
                                    if (match2.Value.Contains("isShowPhone"))
                                    {
                                        Match match3 = new Regex("(?<=<span.*?id=\"isShowPhoneTop\".*?>).*?(?=</span>)").Match(match2.Value);
                                        if (!string.IsNullOrEmpty(match3.Value))
                                        {
                                            info.ContactPhone = match3.Value;
                                        }
                                    }
                                    else
                                    {
                                        info.ContactPhone = match2.Value;
                                    }
                                    if (info.ContactPhone.Trim().Contains("<img"))
                                    {
                                        string value4 = new Regex("(?<=src=\").*?(?=\")").Match(info.ContactPhone).Value;

                                        if (string.IsNullOrEmpty(value4))
                                        {
                                            info.ContactPhone = "";
                                        }
                                        info.ContactPhone = "<img alt=\"phone\" src=\"http://cq.ganji.com" + value4 + "\">";
                                        //using (WebClient webClient = new WebClient())
                                        //{
                                        //    int num = ganjiResource.random.Next(100, 999);
                                        //    string text2 = "/content/resource/phoneImg/" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + num.ToString() + ".png";
                                        //    webClient.Headers.Add("User-Agent", "Chrome");
                                        //    webClient.DownloadFile("http://cq.ganji.com" + value4, AppDomain.CurrentDomain.SetupInformation.ApplicationBase + text2);
                                        //    info.ContactPhone = text2;
                                        //    goto IL_2DA;
                                        //}
                                    }
                                    else
                                    {
                                        info.ContactPhone = info.ContactPhone.NoHTMLLabel();
                                    }
                                }
                                if (string.IsNullOrEmpty(info.ContactPhone))
                                {
                                    this.jumpwithempty++;
                                }
                                else
                                {
                                    info.Address = new Regex("(?<=工作地点：).*?(?=</em>)").Match(text).Value.NoHTMLLabel();
                                    info.PositionType = this.PositionTypeMapping(uri);
                                    info.PositionName = this.PositionNameMapping(uri);
                                    info.Vacancy = new Regex("(?<=招聘人数：.*?<em>).*?(?=</em>)").Match(text).Value.NoHTMLLabel();
                                    info.Salry = new Regex("(?<=薪资待遇：.*?<em>).*?(?=</em>)").Match(text).Value.NoHTMLLabel();
                                    info.Degree = new Regex("(?<=最低学历：.*?<em>).*?(?=</em>)").Match(text).Value.NoHTMLLabel();
                                    info.Experience = new Regex("(?<=工作经验：.*?<em>).*?(?=</em>)").Match(text).Value.NoHTMLLabel();
                                    info.PositionDescript = new Regex("(?<=class=\"deta-Corp\">).*?(?=</div>)").Match(text).Value;
                                    info.PositionDescript = info.PositionDescript.NoHTMLLabel("a").Trim();
                                    string[] forbiddens = this.Forbiddens;
                                    for (int i = 0; i < forbiddens.Length; i++)
                                    {
                                        string value5 = forbiddens[i];
                                        if (!string.IsNullOrEmpty(value5) && info.PositionDescript.IndexOf(value5) > 0)
                                        {
                                            this.jumpwithganji++;
                                        }
                                    }
                                    info.CompanyName = new Regex("(?<=target=\"_blank\"\\s*title=\").*?(?=\")").Match(value).Value.NoHTMLLabel();
                                    info.CopBusiness = new Regex("(?<=公司行业：).*?(?=</span>)").Match(text).Value.NoHTMLLabel();
                                    info.CopType = new Regex("(?<=公司性质：).*?(?=</span>)").Match(text).Value.NoHTMLLabel();
                                    info.CopDimensions = new Regex("(?<=公司规模：).*?(?=</span>)").Match(text).Value.NoHTMLLabel();
                                    info.CopAbout = new Regex("(?<=公司介绍</h4>).*?(?=</p>)").Match(text).Value.NoHTMLLabel();
                                    if (base.PostData(info) == 1)
                                    {
                                        this.recrod++;
                                    }
                                    else
                                    {
                                        this.repeat++;
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Write(0, 0, ex, "");
                    this.jumpwitherror++;
                }
            }
        }
        protected override int PositionTypeMapping(string uri)
        {
            switch (uri)
            {
                case "/jzcuxiaoyuan/":
                    {
                        return 1;
                    }
                case "/jzjianzhimote/":
                    {
                        return 2;
                    }
                case "/jzliyixiaojie/":
                    {
                        return 3;
                    }
                case "/jzjiajiaolaoshi/":
                    {
                        return 4;
                    }
                case "/jzshougongzhizuo/":
                    {
                        return 5;
                    }
                case "/jzfuwuyuan/":
                    {
                        return 6;
                    }
                case "/jzxuesheng/":
                    {
                        return 7;
                    }
                case "/jzchuandanpaifa/":
                    {
                        return 8;
                    }
                case "/jzwangzhanjianshe/":
                    {
                        return 9;
                    }
                case "/jzjianzhikuaiji/":
                    {
                        return 10;
                    }
                case "/jzshixisheng/":
                    {
                        return 11;
                    }
                case "/jzxiaoshigong/":
                    {
                        return 12;
                    }
                case "/jzjianzhifanyi/":
                    {
                        return 13;
                    }
                case "/jzwenjuandiaocha/":
                    {
                        return 14;
                    }
                case "/jzqunzhongyanyuan/":
                    {
                        return 15;
                    }
                case "/jzxiaoshou/":
                    {
                        return 16;
                    }
                case "/jzshejizhizuo/":
                    {
                        return 17;
                    }
                case "/jzsheyingshi/":
                    {
                        return 18;
                    }
                case "/jzjiuba/":
                    {
                        return 19;
                    }
            }
            return 19;
        }
        protected override string PositionNameMapping(string uri)
        {
            switch (uri)
            {
                case "/jzcuxiaoyuan/":
                    {
                        return "促销员";
                    }
                case "/jzjianzhimote/":
                    {
                        return "模特";
                    }
                case "/jzliyixiaojie/":
                    {
                        return "礼仪小姐";
                    }
                case "/jzjiajiaolaoshi/":
                    {
                        return "家教";
                    }
                case "/jzshougongzhizuo/":
                    {
                        return "手工制作员";
                    }
                case "/jzfuwuyuan/":
                    {
                        return "服务员";
                    }
                case "/jzxuesheng/":
                    {
                        return "学生兼职";
                    }
                case "/jzchuandanpaifa/":
                    {
                        return "传单派发员";
                    }
                case "/jzwangzhanjianshe/":
                    {
                        return "网站建设/网站推广员";
                    }
                case "/jzjianzhikuaiji/":
                    {
                        return "会计";
                    }
                case "/jzshixisheng/":
                    {
                        return "实习生";
                    }
                case "/jzxiaoshigong/":
                    {
                        return "小时工";
                    }
                case "/jzjianzhifanyi/":
                    {
                        return "翻译";
                    }
                case "/jzwenjuandiaocha/":
                    {
                        return "问卷调查员";
                    }
                case "/jzqunzhongyanyuan/":
                    {
                        return "群众演员";
                    }
                case "/jzxiaoshou/":
                    {
                        return "销售员";
                    }
                case "/jzshejizhizuo/":
                    {
                        return "设计制作者";
                    }
                case "/jzsheyingshi/":
                    {
                        return "摄影师";
                    }
            }
            return "兼职";
        }
        public override int WorkAreaMapping(string str)
		{
			List<string> source = new List<string>
			{
				"渝中", 
				"沙坪坝", 
				"大学城", 
				"江北", 
				"渝北", 
				"九龙坡", 
				"北碚", 
				"大渡口", 
				"南岸", 
				"巴南", 
				"两江新区", 
				"周边"
			};
			var query = (
				from c in source
				select new
				{
					name = c, 
					except = c.Intersect(str).Count<char>()
				} into c
				orderby c.except descending
				select c).FirstOrDefault();
			if (query.except == 0)
			{
				return 12;
			}
			string name;
            switch (name = query.name)
			{
				case "渝中":
				{
					return 1;
				}
				case "沙坪坝":
				{
					return 2;
				}
				case "大学城":
				{
					return 3;
				}
				case "江北":
				{
					return 4;
				}
				case "渝北":
				{
					return 5;
				}
				case "九龙坡":
				{
					return 6;
				}
				case "北碚":
				{
					return 7;
				}
				case "大渡口":
				{
					return 8;
				}
				case "南岸":
				{
					return 9;
				}
				case "巴南":
				{
					return 10;
				}
				case "两江新区":
				{
					return 11;
				}
			}
			return 12;
		}
        protected override int CompanyBusinessMapping(string orgstr)
        {
            string key;
            switch (key = orgstr.Trim())
            {
                case "教育/培训":
                    {
                        return 22;
                    }
                case "快速消费品(食品/饮料/烟酒/化妆品)":
                    {
                        return 5;
                    }
                case "运动健身":
                    {
                        return 23;
                    }
                case "广告/会展":
                    {
                        return 3;
                    }
                case "咨询/管理产业/法律/财会":
                    {
                        return 33;
                    }
                case "美容美发":
                    {
                        return 15;
                    }
                case "金融/银行/投资/基金/证券":
                    {
                        return 25;
                    }
                case "计算机/网络/通信":
                    {
                        return 9;
                    }
                case "建筑/装修":
                    {
                        return 26;
                    }
                case "超市/百货/零售":
                    {
                        return 5;
                    }
                case "环保":
                    {
                        return 20;
                    }
                case "耐用消费品(服装服饰/纺织/皮革/家具/家电)":
                    {
                        return 37;
                    }
                case "生产/制造":
                    {
                        return 2;
                    }
                case "酒店/餐饮":
                    {
                        return 1;
                    }
                case "媒体/影视/文化":
                    {
                        return 24;
                    }
                case "中介服务":
                    {
                        return 18;
                    }
                case "旅游":
                    {
                        return 27;
                    }
                case "石油/石化/化工":
                    {
                        return 12;
                    }
                case "跨领域经营":
                    {
                        return 38;
                    }
                case "贸易/交通/运输/物流":
                    {
                        return 6;
                    }
                case "农/林/牧/渔":
                    {
                        return 16;
                    }
                case "礼品/玩具/工艺美术/收藏品":
                    {
                        return 36;
                    }
                case "印刷/包装/造纸":
                    {
                        return 11;
                    }
                case "办公用品及设备":
                    {
                        return 5;
                    }
                case "物业管理/商业中心":
                    {
                        return 30;
                    }
                case "医药/生物工程":
                    {
                        return 35;
                    }
                case "保健按摩":
                    {
                        return 19;
                    }
                case "家政":
                    {
                        return 34;
                    }
                case "医疗设备/器械":
                    {
                        return 31;
                    }
            }
            return 39;
        }
        protected override int CompanyTypeMapping(string orgstr)
        {
            string key;
            switch (key = orgstr.Trim())
            {
                case "民营":
                    {
                        return 2;
                    }
                case "上市公司":
                case "合资":
                case "股份制企业":
                    {
                        return 1;
                    }
                case "外商独资":
                    {
                        return 4;
                    }
                case "事业单位":
                case "国企":
                    {
                        return 3;
                    }
            }
            return 5;
        }
        protected override int CompanydimensionsMapping(string orgstr)
        {
            string key;
            switch (key = orgstr.Trim())
            {
                case "20人以下":
                    {
                        return 1;
                    }
                case "20-99人":
                    {
                        return 2;
                    }
                case "100-499人":
                    {
                        return 3;
                    }
            }
            return 4;
        }
        protected override int GenderMapping(string gender)
        {
            return -1;
        }
        protected override int DegreeMapping(string degree)
        {
            string key;
            switch (key = degree.Trim())
            {
                case "高中/中专以上":
                case "大专":
                case "大专及以上":
                case "初中":
                case "初中以上":
                    {
                        return 1;
                    }
                case "本科":
                case "本科及以上":
                    {
                        return 2;
                    }
            }
            return 0;
        }
        protected override int ExperiencedMapping(string experience)
        {
            if (experience == "不限" || string.IsNullOrEmpty(experience))
            {
                return 0;
            }
            return 1;
        }
        protected override int VacancyMapping(string vacancy)
        {
            string value = new Regex("\\d+").Match(vacancy).Value;
            if (string.IsNullOrEmpty(value))
            {
                return 0;
            }
            int result = 0;
            if (int.TryParse(value, out result))
            {
                return result;
            }
            return 0;
        }
    }
}
